package com.gengzp.mq.server.facade.impl;

import com.gengzp.mq.server.facade.MqTopicFacadeService;
import com.gengzp.mq.server.mapper.MqTopicMapper;
import com.gengzp.mq.server.model.topic.entity.MqTopic;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @ClassName MqTopicFacadeServiceImpl
 * @Description 消息主题门面层实现
 * @Author gengzp
 * @Date 2025/10/22 22:08
 */
@Service
public class MqTopicFacadeServiceImpl implements MqTopicFacadeService {

    private static final Logger logger = LoggerFactory.getLogger(MqTopicFacadeServiceImpl.class);

    @Autowired
    private MqTopicMapper mqTopicMapper;

    @Override
    public MqTopic searchTopicByName(String topicName) {
        if (topicName == null || topicName.isBlank()) {
            throw new RuntimeException("主题名称 topicName 不能为空");
        }
        return mqTopicMapper.selectTopicByName(topicName);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void autoCreateTopic(String topicName) {
        MqTopic mqTopic = new MqTopic();
        mqTopic.setTopic(topicName);
        mqTopic.setDescription("该 topic 为发送消息时自动创建");
        mqTopic.setCreatedTime(new Date());
        mqTopicMapper.insertTopic(mqTopic);
    }

}
